program RESTserver;

{$APPTYPE CONSOLE}

{$I Synopse.inc}

// enable/disable third-party libraries
{.$define USENEXUSDB}
{.$define USEBDE}
{.$define USEUNIDAC}
{.$define USEZEOS}
{.$define USEFIREDAC}

// enable/disable database engines
{.$define USEJET}
{.$define USEFIREBIRDEMB}
{.$define ODBCSQLITEFIREBIRD}
{.$define USELOCALMSSQLEXPRESS}    // SQL Server 2008 R2 Express locally
{.$define USELOCALDBMSSQLEXPRESS}  // SQL Server 2012 LocalDB edition
{.$define USELOCALDB2}
{.$define USELOCALPOSTGRESQL}
{.$define USELOCALMYSQL}
{.$define USEMONGODB}

uses
  {$I SynDprUses.inc}  // use FastMM4 on older Delphi, or set FPC threads
  SynCommons,          // framework core
  SynLog,              // logging features
  mORMot,              // RESTful server & ORM
  mORMotSQLite3,       // SQLite3 engine as ORM core
  SynSQLite3Static,    // staticaly linked SQLite3 engine
  mORMotDB,            // ORM using external DB
  mORMotHttpServer,    // HTTP server for RESTful server
  SynDB,               // external DB core
  SynDBODBC,           // external DB access via ODBC
  RESTModel,           // data model unit, shared between server and client

  BancoDTO in '..\Comum\DTO\BancoDTO.pas',
  Atributos in '..\Comum\Atributos.pas',
  Constantes in '..\Comum\Constantes.pas',
  PessoaContatoDTO in '..\Comum\DTO\PessoaContatoDTO.pas',
  PessoaDTO in '..\Comum\DTO\PessoaDTO.pas',
  PessoaEnderecoDTO in '..\Comum\DTO\PessoaEnderecoDTO.pas',
  PessoaFisicaDTO in '..\Comum\DTO\PessoaFisicaDTO.pas',
  PessoaJuridicaDTO in '..\Comum\DTO\PessoaJuridicaDTO.pas',
  PessoaTelefoneDTO in '..\Comum\DTO\PessoaTelefoneDTO.pas';

var
  aModel: TSQLModel;
  aProps: TSQLDBConnectionProperties;
  aRestServer: TSQLRestServerDB;
  aHttpServer: TSQLHttpServer;
begin
  // set logging abilities
  SQLite3Log.Family.Level := LOG_VERBOSE;
  //SQLite3Log.Family.EchoToConsole := LOG_VERBOSE;
  SQLite3Log.Family.PerThreadLog := ptIdentifiedInOnFile;

  aProps := TODBCConnectionProperties.Create('',
  'Driver=MySQL ODBC 5.3 UNICODE Driver;Database=t2tierp;'+
  'Server=localhost;Port=3306;UID=root;Pwd=root','','');

  try

    // get the shared data model
    aModel := TSQLModel.Create([TPessoa, TBanco, TPessoa_Telefone, TPessoa_Endereco, TPessoa_Contato, TPessoa_Fisica, TPessoa_Juridica], 'root');

    // use MySQL database for all tables
    VirtualTableExternalRegisterAll(aModel, aProps);

    try
      // create the main mORMot server
      aRestServer := TSQLRestServerDB.Create(aModel,':memory:',false);
      try
        /// EXERCICIO: Como evitar que o mORMot crie a coluna PK nas tabelas do banco de dados?
        // create tables or fields if missing
        aRestServer.CreateMissingTables(0, [itoNoCreateMissingField]);

        // serve aRestServer data over HTTP
        aHttpServer := TSQLHttpServer.Create('888',[aRestServer],'+',useHttpApiRegisteringURI);
        try
          aHttpServer.AccessControlAllowOrigin := '*'; // allow cross-site AJAX queries
          writeln('Background server is running.'#10);
          write('Press [Enter] to close the server.');
          readln;
        finally
          aHttpServer.Free;
        end;
      finally
        aRestServer.Free;
      end;
    finally
      aModel.Free;
    end;
  finally
    aProps.Free;
  end;
end.
